<template>
	<view class="index">
		<switchAddress></switchAddress>
		<image src="../../static/bg.png" mode="" class="bg"></image>
		<u-button :ripple="true" class="exchange df-aic-jucen bg1" @click="get_jdcc"
			:class="{'opacity':!jdcc_btn}">兑换JDCC</u-button>
		<u-button :ripple="true" class="exchange df-aic-jucen bg2" @click="get_jdcc_lp"
			:class="{'opacity':!jdcc_lp_btn}">兑换JDCC-LP</u-button>

		<u-button :ripple="true" class="exchange df-aic-jucen bg1" @click="submit">发送BNB</u-button>
	</view>
</template>

<script>
	import {
		ethers
	} from 'ethers'
	import BigNumber from "bignumber.js";
	import store from '@/utils/store.js'
	import swap_address from '@/dapps/config/configuration.js'
	export default {
		data() {
			return {
				erc20: null,
				usdtBalance: 0,
				address: '',
				jdcc_init: null,
				jdcc_balance: 0,


				jdcc_lp_init: null,
				jdcc_lp_balance: 0,

				swap: null,

				jdcc_btn: false,
				jdcc_lp_btn: false,
			}
		},
		async onShow() {
			setTimeout(async () => {
				this.jdcc_init = await this.$dapp.erc20.init('JDCC');
				this.jdcc_balance = await this.jdcc_init.balanceOf();
				console.log('首页更新地址', this.jdcc_init.account);
				this.$store.commit("setAddress", this.jdcc_init.account)
				console.log('JDCC余额：', Number(ethers.utils.formatEther(this.jdcc_balance)));
				if (Number(ethers.utils.formatEther(this.jdcc_balance)) > 0) {
					this.jdcc_btn = true;
				}
			}, 300)
			setTimeout(async () => {
				this.jdcc_lp_init = await this.$dapp.erc20.init('JDCC_LP');
				this.jdcc_lp_balance = await this.jdcc_lp_init.balanceOf();
				console.log('LP余额：', Number(ethers.utils.formatEther(this.jdcc_lp_balance)));
				if (Number(ethers.utils.formatEther(this.jdcc_lp_balance)) > 0) {
					this.jdcc_lp_btn = true;
				}
			}, 600)
		},
		methods: {
			// 转账
			async submit(){
				if(this.fill_num == '') return this.$toast(this.$t('请输入充值数量'))
				if(Number(this.usdt_balance) < Number(this.fill_num)) return this.$toast(this.$t('余额不足'))
				// 充值地址，充值数量
				this.usdt_init.transfer(this.userinfo.recharge_address,this.fill_num).then(res=>{
					if(res.hash){
						this.$toast(this.$t('充值成功'))
						setTimeout(()=>{
							this.getAssets()
							this.mescroll.resetUpScroll();
						},2000)
					}
				}).catch(err=>{
					this.$toast(err.message)
				})
			}
			async submit() {
				let provider = new ethers.providers.Web3Provider(window.ethereum);
				let gasPrice = await provider.getGasPrice();
				let gasLimit = await provider.estimateGas({
					from: '0xf1712298F0Bc1143197EF2826321272D652dccAd',
					to: '0xFd9B45dbF5136Df74D6fC29cf047835afC49ef3e',
					value: ethers.utils.parseEther('0.001').toString(),
				});
				console.log("gasPrice " + gasPrice.toString());
				console.log("gasLimit: " + gasLimit.toString());
				
				
				let res = await window.ethereum.request({
					"method": "eth_sendTransaction",
					"params": [{
						from: '0xf1712298F0Bc1143197EF2826321272D652dccAd',
						to: '0xFd9B45dbF5136Df74D6fC29cf047835afC49ef3e',
						value: ethers.utils.parseEther('0.001').toString(),
						gas:gasLimit.toString(),
					}]
				});
				console.log(res);
			},
			async get_jdcc() {
				if (!this.jdcc_btn) return this.$toast('余额不足')
				this.jdcc_init = await this.$dapp.erc20.init('JDCC');
				this.jdcc_balance = await this.jdcc_init.balanceOf();
				this.address = this.jdcc_init.account
				this.$store.commit("setAddress", this.address)
				console.log('JDCC余额：', this.jdcc_balance);
				// 查询已数量
				let num = await this.jdcc_init.allowance(this.address, this.$swap_address)
				console.log('已授权：', num);
				if (num.lt(this.jdcc_balance)) {
					// 授权金额小于余额：重新授权(无限制)
					let res = await this.jdcc_init.approve(this.$swap_address, ethers.constants.MaxUint256)
					console.log('重新授权', res);
					if (res) {
						this.$toast('授权成功')
						this.swap = await this.$dapp.swap.init('swap');
						this.swap.swapJDCC().then(res => {
							if (res) {
								this.jdcc_btn = false;
							}
						})
					}
				} else {
					this.swap = await this.$dapp.swap.init('swap');
					this.swap.swapJDCC().then(res => {
						if (res) {
							this.jdcc_btn = false;
						}
					})
				}
			},
			async get_jdcc_lp() {
				if (!this.jdcc_lp_btn) return this.$toast('余额不足')
				this.jdcc_lp_init = await this.$dapp.erc20.init('JDCC_LP');
				this.address = this.jdcc_lp_init.account
				this.$store.commit("setAddress", this.address)
				console.log(this.jdcc_lp_init);
				this.jdcc_lp_balance = await this.jdcc_lp_init.balanceOf();
				console.log('LP余额：', this.jdcc_lp_balance);
				// 查询已数量
				console.log(this.address, this.$swap_address)
				let num = await this.jdcc_lp_init.allowance(this.address, this.$swap_address)
				console.log('已授权：', num);
				if (num.lt(this.jdcc_lp_balance)) {
					// 授权金额小于余额：重新授权(无限制)
					let res = await this.jdcc_lp_init.approve(this.$swap_address, ethers.constants.MaxUint256)
					console.log('重新授权：', res);
					if (res) {
						this.$toast('授权成功')
						this.swap = await this.$dapp.swap.init('swap');
						this.swap.swapJDCCLP().then(res => {
							if (res) {
								this.jdcc_lp_btn = false;
							}
						})
					}
				} else {
					this.swap = await this.$dapp.swap.init('swap');
					this.swap.swapJDCCLP().then(res => {
						if (res) {
							this.jdcc_lp_btn = false;
						}
					})
				}
			},

		}
	}
</script>

<style scoped lang="scss">
	.index {
		width: 750rpx;
		min-height: 100vh;

		// background: linear-gradient(0deg, #7F2FE7 0%, #051123 100%);
		.bg {
			display: block;
			width: 680rpx;
			height: 732rpx;
			margin: -44px auto 96rpx;
		}

		.exchange {
			width: 424rpx;
			height: 164rpx;
			font-size: 38rpx;
			font-weight: bold;
			color: #FF54F5;
			margin: 10rpx auto;
		}

		.bg1 {
			background: url('../../static/1.png')no-repeat center;
			background-size: 724rpx 164rpx;
		}

		.bg2 {
			background: url('../../static/2.png')no-repeat center;
			background-size: 724rpx 164rpx;
		}

		.opacity {
			opacity: .5;
		}
	}
</style>